<?php class models extends Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function truncate(){
    	return $this->db->truncate('models');
    }
    
    function insert_for_excel($array){
       $this->name = $array['name'];
       $this->createtime = time();
       $re = $this->select_array(array('name'=>$array['name']));
       if(count($re)==0){
         $this->db->insert('models',$this);
         $result =$this->db->insert_id();
       }else{
         $result = $re[0]->mod_id;
       }
       return $result;
    }
    
    function insert(){
       $this->name = $_POST['name'];
       $this->des = $_POST['des'];
       $this->createtime = time();
       $re = $this->auth($_POST['name']);
       if($re==0){
         $result = $this->db->insert('models',$this);
       }else{
         $result = true;
       }
       return $result;
    }
    
    function auth($name){
    	$this->db->where(array('name'=>trim($name)));
    	$this->db->from('models');
        $result = $this->db->count_all_results(); 
        return $result;
    }
    
    function select_array($array){
    	$this->db->where($array);
    	$query = $this->db->get('models');
        $result = $query->result(); 
        return $result;
    }
    
    function select($id,$num="",$offset=""){
       $this->db->limit($num,$offset);	
       if($id!=""){
       	  $this->db->where(array('mod_id'=>$id));
       }
       $query = $this->db->get('models');
       $result = $query->result(); 
       return $result;
    }
    
    function update($id){
       $this->name = $_POST['name'];
       $this->des = $_POST['des'];
       $result = $this->db->update('models',$this,array('mod_id'=>$id));
       return $result;
    }
    
    function del($id){
       $result = $this->db->delete('models',array('mod_id'=>$id));
       return $result;
    }
}